home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / PowerPlant / 3D Additions 1.7 / 3D Additions / 3DUtilities.cp < prev    next >
Encoding:
Text File  |  1995-10-09  |  16.0 KB  |  595 lines  |  [TEXT/CWIE]

  1. // ===========================================================================
  2. // 3DUtilities.cp      ©1995 J. Rodden, DD/MF & Associates. All rights reserved
  3. // ===========================================================================
  4. // Utilities for drawing 3D effects.
  5. //
  6. // This is intended to look and act as much like a Toolbox Manager as possible.
  7. // You should use the first set of procedures in general, but the second set 
  8. // is directly available for flexibility.
  9. //
  10. // This source code is loosely based on and heavily inspired by source code
  11. // by James W. Osborne, copyright (c) 1993, Apple Computer.
  12.  
  13. #include "3DUtilities.h"
  14.  
  15. // ===========================================================================
  16. // Local variables. Statics are used rather than a class to allow ANSI C usage.
  17. // ===========================================================================
  18.  
  19. static RGBColor theLightColor    = kWhite;
  20. static RGBColor theShadowColor    = kShadowGray;
  21. static RGBColor theBkgndColor    = kBackgroundGray;
  22. static PenState    thePnState        = {{0,0},{1,1},srcCopy,
  23.                                     {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}};
  24.  
  25. // ===========================================================================
  26. // Static function prototypes.
  27. // ===========================================================================
  28.  
  29. static PenState    theOldPnState;    // only used by static functions.
  30.  
  31. static void    GetCPen();
  32. static void    GetBWPen();
  33. static void    SavePenState();
  34. static void    RestorePenState();
  35.  
  36.  
  37. // ===========================================================================
  38. // Accessor functions to 3D colors.
  39. // ===========================================================================
  40.  
  41. // • Get 3D State
  42. void Get3DBackColor( RGBColor *outBkgndColor)    { *outBkgndColor = theBkgndColor; }
  43.  
  44. void Get3DLightColor( RGBColor *outLightColor)    { *outLightColor = theLightColor; }
  45.     
  46. void Get3DShadowColor( RGBColor *outShadowColor){ *outShadowColor= theShadowColor; }
  47.  
  48. void Get3DPenState( PenState *outPnState)        { *outPnState     = thePnState; }
  49.  
  50.  
  51. // • Set 3D State
  52. void Set3DBackColor(const RGBColor *inBkgndColor)    { theBkgndColor     = *inBkgndColor; }
  53.  
  54. void Set3DLightColor(const RGBColor *inLightColor)    { theLightColor     = *inLightColor; }
  55.  
  56. void Set3DShadowColor(const RGBColor *inShadowColor){ theShadowColor = *inShadowColor; }
  57.  
  58. void Set3DPenState(const PenState *inPnState)        { thePnState     = *inPnState; }
  59.  
  60.  
  61. // ===========================================================================
  62. // ===========================================================================
  63.  
  64. // ---------------------------------------------------------------------------
  65. //        • Draw3DOvalPanel
  66. // ---------------------------------------------------------------------------
  67. // Draw a 3D oval panel (raised or embedded) in the current grafPort
  68. // using the current PenState.
  69.  
  70. void    Draw3DOvalPanel(
  71.     const Rect*        inRect,
  72.     const RGBColor*    inBKColor,
  73.     const RGBColor*    inULColor,
  74.     const RGBColor*    inLRColor,
  75.     const Boolean    inFrameIt)
  76. {
  77.     RGBColor    theForeColor;
  78.  
  79.     GetForeColor( &theForeColor);
  80.  
  81.     // Paint background color
  82.     RGBForeColor(inBKColor);
  83.     PaintOval(inRect);
  84.  
  85.     // Draw oval border
  86.     Draw3DOvalBorder( inRect, inULColor, inLRColor, inFrameIt);
  87.  
  88.     RGBForeColor( &theForeColor);
  89. }
  90.  
  91. // ===========================================================================
  92.  
  93. // • Draw3DInsetOvalPanel
  94. void Draw3DInsetOvalPanel(const Rect *inRect)
  95. {
  96.     SavePenState();
  97.     GetCPen();
  98.     Draw3DOvalPanel(inRect, &theLightColor, &theShadowColor, &theLightColor, true);
  99.     RestorePenState();
  100. }
  101.  
  102. // • Draw3DRaisedOvalPanel
  103. void Draw3DRaisedOvalPanel(const Rect *inRect)
  104. {
  105.     SavePenState();
  106.     GetCPen();
  107.     Draw3DOvalPanel(inRect, &theBkgndColor, &theLightColor, &theShadowColor, false);
  108.     RestorePenState();
  109. }
  110.  
  111.  
  112. // ===========================================================================
  113. // ===========================================================================
  114.  
  115. // ---------------------------------------------------------------------------
  116. //        • Draw3DOvalBorder
  117. // ---------------------------------------------------------------------------
  118. // Draw a 3D oval border (raised or embedded) in the current grafPort
  119. // using the current PenState.
  120.  
  121. void    Draw3DOvalBorder(
  122.     const Rect*        inRect,
  123.     const RGBColor*    inULColor,
  124.     const RGBColor*    inLRColor,
  125.     const Boolean    inFrameIt)
  126. {
  127.     RGBColor    theForeColor;
  128.  
  129.     GetForeColor( &theForeColor);
  130.  
  131.     // Draw upper left color
  132.     RGBForeColor(inULColor);
  133.     FrameOval(inRect);
  134.     
  135.     // Draw lower right color
  136.     RGBForeColor(inLRColor);
  137.     FrameArc( inRect, 45, 180);
  138.  
  139.     // Draw inner oval (frame)
  140.     if (inFrameIt) {
  141.         Rect tempRect = *inRect;
  142.         InsetRect(&tempRect,thePnState.pnSize.h,thePnState.pnSize.v);
  143.         RGBForeColor(&kBlack); FrameOval(&tempRect);
  144.     }
  145.  
  146.     RGBForeColor( &theForeColor);
  147. }
  148.  
  149. // ===========================================================================
  150.  
  151. // • Draw3DInsetOvalBorder
  152. void Draw3DInsetOvalBorder(const Rect *inRect)
  153. {
  154.     SavePenState();
  155.     GetCPen();
  156.     Draw3DOvalBorder(inRect, &theShadowColor, &theLightColor, false);
  157.     RestorePenState();
  158. }
  159.  
  160. // • Draw3DRaisedOvalBorder
  161. void Draw3DRaisedOvalBorder(const Rect *inRect)
  162. {
  163.     SavePenState();
  164.     GetCPen();
  165.     Draw3DOvalBorder(inRect, &theLightColor, &theShadowColor, false);
  166.     RestorePenState();
  167. }
  168.  
  169.  
  170. // ===========================================================================
  171. // ===========================================================================
  172.  
  173. // ---------------------------------------------------------------------------
  174. //        • Draw3DOvalFrame
  175. // ---------------------------------------------------------------------------
  176. // Draw a 3D oval frame (raised or embedded) in the current grafPort
  177. // using the current PenState.
  178.  
  179. void    Draw3DOvalFrame(
  180.     const Rect*        inRect,
  181.     const RGBColor*    inULColor,
  182.     const RGBColor*    inLRColor)
  183. {
  184.     Rect tempRect = *inRect;
  185.     RGBColor theForeColor;
  186.  
  187.     GetForeColor( &theForeColor);
  188.  
  189.     RGBForeColor(inULColor);
  190.     FrameOval(&tempRect);
  191.     
  192.     OffsetRect(&tempRect,thePnState.pnSize.h, thePnState.pnSize.v);
  193.                          
  194.     RGBForeColor(inLRColor);
  195.     FrameOval(&tempRect);
  196.  
  197.     RGBForeColor( &theForeColor);
  198. }
  199.  
  200. // ===========================================================================
  201.  
  202. // • Draw3DInsetOvalFrame
  203. void Draw3DInsetOvalFrame(const Rect *inRect)
  204. {
  205.     SavePenState();
  206.     GetCPen();
  207.     Draw3DOvalFrame(inRect, &theShadowColor, &theLightColor);
  208.     RestorePenState();
  209. }
  210.  
  211. // • Draw3DRaisedOvalFrame
  212. void Draw3DRaisedOvalFrame(const Rect *inRect)
  213. {
  214.     SavePenState();
  215.     GetCPen();
  216.     Draw3DOvalFrame(inRect, &theLightColor, &theShadowColor);
  217.     RestorePenState();
  218. }
  219.  
  220.  
  221. // ===========================================================================
  222. // ===========================================================================
  223.  
  224. // ---------------------------------------------------------------------------
  225. //        • Draw3DPanel
  226. // ---------------------------------------------------------------------------
  227. // Draw a 3D panel (raised or embedded) in the current grafPort
  228. // using the current PenState.
  229.  
  230. void    Draw3DPanel(
  231.     const Rect*        inRect,
  232.     const RGBColor*    inBKColor,
  233.     const RGBColor*    inULColor,
  234.     const RGBColor*    inLRColor,
  235.     const Boolean    inFrameIt)
  236. {
  237.     RGBColor    theForeColor;
  238.  
  239.     GetForeColor( &theForeColor);
  240.  
  241.     // Paint background color
  242.     RGBForeColor(inBKColor);
  243.     PaintRect(inRect);
  244.  
  245.     Draw3DBorder( inRect, inULColor, inLRColor, inFrameIt);
  246.  
  247.     RGBForeColor( &theForeColor);
  248. }
  249.  
  250. // ===========================================================================
  251.  
  252. // • Draw3DInsetPanel
  253. void Draw3DInsetPanel(const Rect *inRect)
  254. {
  255.     SavePenState();
  256.     GetCPen();
  257.     Draw3DPanel(inRect, &theLightColor, &theShadowColor, &theLightColor, true);
  258.     RestorePenState();
  259. }
  260.  
  261. // • Draw3DRaisedPanel
  262. void Draw3DRaisedPanel(const Rect *inRect)
  263. {
  264.     SavePenState();
  265.     GetCPen();
  266.     Draw3DPanel(inRect, &theBkgndColor, &theLightColor, &theShadowColor, false);
  267.     RestorePenState();
  268. }
  269.  
  270.  
  271. // ===========================================================================
  272. // ===========================================================================
  273.  
  274. // ---------------------------------------------------------------------------
  275. //        • Draw3DBorder
  276. // ---------------------------------------------------------------------------
  277. // Draw a 3D panel border (raised or embedded) in the current grafPort
  278. // using the current PenState.
  279.  
  280. void    Draw3DBorder(
  281.     const Rect*        inRect,
  282.     const RGBColor*    inULColor,
  283.     const RGBColor*    inLRColor,
  284.     const Boolean    inFrameIt)
  285. {
  286.     PenState    thePnState;
  287.     RGBColor    theForeColor;
  288.     
  289.     GetPenState( &thePnState);
  290.     GetForeColor( &theForeColor);
  291.  
  292.     // Draw lower right (shadow) lines
  293.     RGBForeColor(inLRColor);
  294.     FrameRect(inRect);
  295.  
  296.     // Draw upper left (light source) lines
  297.     RGBForeColor(inULColor);
  298.     MoveTo( inRect->left, inRect->bottom - thePnState.pnSize.v);
  299.     LineTo( inRect->left, inRect->top);
  300.     LineTo( inRect->right - thePnState.pnSize.h, inRect->top);
  301.     
  302.     // Draw inner box (frame)
  303.     if (inFrameIt) {
  304.         Rect tempRect = *inRect;
  305.         InsetRect(&tempRect,thePnState.pnSize.h,thePnState.pnSize.v);
  306.         RGBForeColor(&kBlack);
  307.         FrameRect(&tempRect);
  308.     }
  309.     
  310.     RGBForeColor(&theForeColor);
  311. }
  312.  
  313. // ===========================================================================
  314.  
  315. // • Draw3DInsetBorder
  316. void    Draw3DInsetBorder(const Rect *inRect)
  317. {
  318.     SavePenState();
  319.     GetCPen();
  320.     Draw3DBorder(inRect, &theShadowColor, &theLightColor, false);
  321.     RestorePenState();
  322. }
  323.  
  324. // • Draw3DRaisedBorder
  325. void    Draw3DRaisedBorder(const Rect *inRect)
  326. {
  327.     SavePenState();
  328.     GetCPen();
  329.     Draw3DBorder(inRect, &theLightColor, &theShadowColor, false);
  330.     RestorePenState();
  331. }
  332.  
  333.  
  334. // ===========================================================================
  335. // ===========================================================================
  336.  
  337. // ---------------------------------------------------------------------------
  338. //        • Draw3DFrame
  339. // ---------------------------------------------------------------------------
  340. // Draw a 3D rect (raised or embedded) in the current grafPort using the
  341. // current PenState.
  342.  
  343. void
  344. Draw3DFrame(
  345.     const Rect*        inRect,
  346.     const RGBColor*    inULColor,
  347.     const RGBColor*    inLRColor)
  348. {
  349.     Rect        tempRect = *inRect;
  350.     PenState    thePnState;
  351.     RGBColor    theForeColor;
  352.     
  353.     GetPenState(&thePnState);
  354.     GetForeColor(&theForeColor);
  355.  
  356.     tempRect.right -= thePnState.pnSize.h;
  357.     tempRect.bottom -= thePnState.pnSize.v;
  358.     
  359.     RGBForeColor(inULColor);
  360.     FrameRect(&tempRect);
  361.     
  362.     OffsetRect(&tempRect,thePnState.pnSize.h, thePnState.pnSize.v);
  363.                          
  364.     RGBForeColor(inLRColor);
  365.     FrameRect(&tempRect);
  366.  
  367.     RGBForeColor(&theForeColor);
  368. }
  369.  
  370. // ===========================================================================
  371.  
  372. // • Draw3DInsetFrame
  373. void Draw3DInsetFrame(const Rect *inRect)
  374. {
  375.     SavePenState();
  376.     GetCPen();
  377.     Draw3DFrame(inRect, &theShadowColor, &theLightColor);
  378.     RestorePenState();
  379. }
  380.  
  381. // • Draw3DRaisedFrame
  382. void Draw3DRaisedFrame(const Rect *inRect)
  383. {
  384.     SavePenState();
  385.     GetCPen();
  386.     Draw3DFrame(inRect, &theLightColor, &theShadowColor);
  387.     RestorePenState();
  388. }
  389.  
  390.  
  391. // ===========================================================================
  392. // ===========================================================================
  393.  
  394. // ---------------------------------------------------------------------------
  395. //        • Draw3DHLine
  396. // ---------------------------------------------------------------------------
  397. // Draw a 3D line (raised or embedded) horizontally accross the current grafPort
  398. // using the current PenState.
  399.  
  400. void
  401. Draw3DHLine(const short vpos, const short h1, const short h2,
  402.             const RGBColor *inULColor, const RGBColor *inLRColor)
  403. {
  404.     PenState    thePnState;
  405.     RGBColor    theForeColor;
  406.     
  407.     GetPenState( &thePnState);
  408.     GetForeColor( &theForeColor);
  409.  
  410.     DrawCLine( inULColor, h1, vpos, h2, vpos);
  411.     DrawCLine( inLRColor, h1, vpos+thePnState.pnSize.v,
  412.                           h2, vpos+thePnState.pnSize.v);
  413.  
  414.     RGBForeColor( &theForeColor);
  415. }
  416.  
  417. // ===========================================================================
  418.  
  419. // • Draw3DInsetHLine
  420. void Draw3DInsetHLine(const short vpos, const short h1, const short h2)
  421. {
  422.     SavePenState();
  423.     GetCPen();
  424.     Draw3DHLine( vpos, h1, h2, &theShadowColor, &theLightColor);
  425.     RestorePenState();
  426. }
  427.  
  428. // • Draw3DRaisedHLine
  429. void Draw3DRaisedHLine(const short vpos, const short h1, const short h2)
  430. {
  431.     SavePenState();
  432.     GetCPen();
  433.     Draw3DHLine( vpos, h1, h2, &theLightColor, &theShadowColor);
  434.     RestorePenState();
  435. }
  436.  
  437.  
  438. // ===========================================================================
  439. // ===========================================================================
  440.  
  441. // ---------------------------------------------------------------------------
  442. //        • Draw3DVLine
  443. // ---------------------------------------------------------------------------
  444. // Draw a 3D line (raised or embedded) vertically accross the current grafPort
  445. // using the current PenState.
  446.  
  447. void
  448. Draw3DVLine(const short hpos, const short v1, const short v2,
  449.             const RGBColor *inULColor, const RGBColor *inLRColor)
  450. {
  451.     PenState    thePnState;
  452.     RGBColor    theForeColor;
  453.     
  454.     GetPenState( &thePnState);
  455.     GetForeColor( &theForeColor);
  456.  
  457.     DrawCLine( inULColor, hpos, v1, hpos, v2);
  458.     DrawCLine( inLRColor, hpos+thePnState.pnSize.h, v1,
  459.                           hpos+thePnState.pnSize.h, v2);
  460.  
  461.     RGBForeColor( &theForeColor);
  462. }
  463.  
  464. // ===========================================================================
  465.  
  466. // • Draw3DInsetVLine
  467. void Draw3DInsetVLine(const short hpos, const short v1, const short v2)
  468. {
  469.     SavePenState();
  470.     GetCPen();
  471.     Draw3DVLine( hpos, v1, v2, &theShadowColor, &theLightColor);
  472.     RestorePenState();
  473. }
  474.  
  475. // • Draw3DRaisedVLine
  476. void Draw3DRaisedVLine(const short hpos, const short v1, const short v2)
  477. {
  478.     SavePenState();
  479.     SavePenState();
  480.     GetCPen();
  481.     Draw3DVLine( hpos, v1, v2, &theLightColor, &theShadowColor);
  482.     RestorePenState();
  483. }
  484.  
  485.  
  486. // ===========================================================================
  487. // ===========================================================================
  488.  
  489. // ---------------------------------------------------------------------------
  490. //        • DrawCLine
  491. // ---------------------------------------------------------------------------
  492. // Draw a colored line within the current grafPort using the current PenState.
  493.  
  494. void
  495. DrawCLine(const RGBColor *theColor,
  496.           const short h1, const short v1, const short h2, const short v2)
  497. {
  498.     MoveTo( h1, v1);
  499.     CLineTo( theColor, h2, v2);
  500. }
  501.  
  502. // ---------------------------------------------------------------------------
  503. //        • CLineTo
  504. // ---------------------------------------------------------------------------
  505. // Draw a colored line within the current grafPort using the current PenState.
  506.  
  507. void
  508. CLineTo(const RGBColor *theColor, const short h, const short v)
  509. {
  510.     RGBForeColor(theColor);
  511.     LineTo( h, v);
  512. }
  513.  
  514.  
  515. // ===========================================================================
  516. // ===========================================================================
  517.  
  518. // ---------------------------------------------------------------------------
  519. //        • BWShadowInset
  520. // ---------------------------------------------------------------------------
  521.  
  522. void
  523. BWShadowInset(const Rect* inRect)
  524. {
  525.     if ( inRect != nil ) {
  526.         SavePenState();
  527.         GetBWPen();
  528.     
  529.         MoveTo( inRect->left, inRect->bottom);
  530.         LineTo( inRect->left, inRect->top);
  531.         LineTo( inRect->right, inRect->top);
  532.     
  533.         RestorePenState();
  534.     }
  535. }
  536.  
  537.  
  538. // ---------------------------------------------------------------------------
  539. //        • BWShadowRaised
  540. // ---------------------------------------------------------------------------
  541.  
  542. void
  543. BWShadowRaised(const Rect* inRect)
  544. {
  545.     if ( inRect != nil ) {
  546.         SavePenState();
  547.         GetBWPen();
  548.     
  549.         MoveTo( inRect->left, inRect->bottom);
  550.         LineTo( inRect->right, inRect->bottom);
  551.         LineTo( inRect->right, inRect->top);
  552.     
  553.         RestorePenState();
  554.     }
  555. }
  556.  
  557.  
  558. // ---------------------------------------------------------------------------
  559. //        • BWShadowLine
  560. // ---------------------------------------------------------------------------
  561. // Draw a BW shadowed line within the current grafPort using the current PenState.
  562.  
  563. void
  564. BWShadowLine(const short h1, const short v1, const short h2, const short v2)
  565. {
  566.     SavePenState();
  567.     GetBWPen();
  568.     
  569.     MoveTo( h1, v1);
  570.     LineTo( h2, v2);
  571.     
  572.     RestorePenState();
  573. }
  574.  
  575.  
  576. // ===========================================================================
  577. // ===========================================================================
  578.  
  579. void GetCPen()
  580. {    SetPenState( &thePnState); }
  581.  
  582. void GetBWPen()
  583. {
  584.     Pattern grey = {0xFFFFFFE8};
  585.     GetCPen();
  586.     PenPat(&grey);
  587. }
  588.  
  589. void SavePenState()
  590. {    GetPenState( &theOldPnState); }
  591.  
  592. void RestorePenState()
  593. {    SetPenState( &theOldPnState); }
  594.  
  595.